import config from './config/config.dev'
let {SMTP_SERVER, SMTP_EMAIL, SMTP_PASSWORD} = config

import nodemailer from 'nodemailer'
import SMTPTransport from 'nodemailer/lib/smtp-transport';

const getTransporter = () => nodemailer.createTransport({
    secure: true,
    host: SMTP_SERVER,
    auth: {
        user: SMTP_EMAIL,
        pass: SMTP_PASSWORD
    }
});
let transporter = getTransporter()
export const emailTo = (email: string, title: string, content: string, type: "text" | "html") => {
    let mailOptions = {
        from: SMTP_EMAIL,
        to: email,
        subject: title
    } as any
    switch (type) {
        case 'html':
            mailOptions.html = content
            break;
        case 'text':
            mailOptions.text = content
            break;
    }
    return new Promise<SMTPTransport.SentMessageInfo>((res, rej) => {
        transporter.sendMail(mailOptions, (err: Error, info:SMTPTransport.SentMessageInfo) => {
            if (err) rej(err)
            else res(info)
        })
    })
}
